package com.evola.conditionalformatting
{
	import com.generic.alerts.GenericAlert;

	public class ViewValue
	{

		public static const TYPE_VARIABLE:int=1;
		public static const TYPE_CONST:int=2;
		public static const TYPE_PLACEHOLDER:int=3;

		public static const NUMERIC:String="n";
		public static const STRING:String="s";
		public static const DATE:String="d";
		public static const BOOLEAN:String="b";
		public static const OBJECT:String="o";

		public var value:String;
		public var dataType:String;
		public var type:int;

		public function ViewValue()
		{
		}

		public function toExpressionString():String
		{

			var exp:String="";

			if (type == TYPE_VARIABLE)
				exp+="@";
			if (type == TYPE_PLACEHOLDER)
				exp+="%";

			exp+=value;

			if (dataType)
				exp+="(" + dataType + ")";

			return exp;
		}

		public function getRealValue(dataObject:Object):Object
		{

			var val:*;

			if (type == ViewValue.TYPE_CONST)
			{
				val=value;
			}

			if (type == ViewValue.TYPE_PLACEHOLDER)
			{

				if (ConditionalFormattingSettings.PLACEHOLDER_FUNCTION == null)
					throw new Error("Placeholder function is not set on ConditionalFormattingSettings.");

				val=ConditionalFormattingSettings.PLACEHOLDER_FUNCTION(value, dataObject);
			}

			if (type == ViewValue.TYPE_VARIABLE)
			{

				//sada znamo da je u pitanju varijabla iz value objekta
				if (dataObject is XML)
				{
					val=(dataObject as XML).attribute(value);
				}
				else
				{
					//ovde se moze desiti da imamo slozen naziv promenjive tj. propertyChain npr. project.status
					//u tom slucaju moramo izvuci vrednost iz poslednje promenjive iz lanca
					var dotIndex:int=value ? value.indexOf(".") : -1;

					if (dotIndex >= 0)
					{

						var firstProp:String=value.substring(0, dotIndex);
						var secondProp:String=value.substr(dotIndex + 1);

						var firstValue:Object=dataObject[firstProp];

						if (firstValue)
						{
							if (firstValue.hasOwnProperty(secondProp))
								val=firstValue[secondProp];
							else
								GenericAlert.error("Property '" + secondProp + "' was not found on object " + firstValue + ". Property chain is: " + value);
						}
					}
					else
					{

						val=dataObject[value];
					}
				}

				if (val != null)
				{

					if (ConditionalFormattingSettings.OBJECT_CONVERSION_FUNCTION != null)
					{

						val=ConditionalFormattingSettings.OBJECT_CONVERSION_FUNCTION(val);
					}

					//radimo konverziju podatka
					if (dataType == ViewValue.STRING && !(val is String))
					{
						val=String(val);
					}

					if (dataType == ViewValue.DATE)
					{
						if (!(val is Date))
							val=val ? Utils.stringToDate(val) : null;

						//moramo datumu izbaciti time vrednosti
						if (val is Date)
						{
							val=new Date(val.getFullYear(), val.getMonth(), val.getDate());
						}
					}

					if (dataType == ViewValue.NUMERIC && !(val is Number))
					{
						val=Number(val);
					}

					if (dataType == ViewValue.BOOLEAN && !(val is Boolean))
					{
						val=String(val) == "true";
					}
				}

			}

			return val;
		}

	}
}


